import { queryParams } from '../utils/util.js'
Component({

  properties: {

  },

  data: {
    selectedIndex : 0,
    tabList : [
      {
        text : '附近',
        name : 'home',
        pagePath: "/pages/index/index",
        iconPath: "./images/near.png",
        selectedIconPath: "./images/near_act.png"
      },
      {
        text: '充电中',
        name: 'charging',
        pagePath: "/pages/charging/index",
        iconPath: "./images/charging.png",
        selectedIconPath: "./images/charging_act.png"
      },
      {
        text: '扫码',
        name: 'scan',
        pagePath: "",
        iconPath: "./images/scan.png",
        selectedIconPath: "./images/scan.png"
      },
      {
        text: '订单',
        name: 'order',
        pagePath: "/pages/order/index",
        iconPath: "./images/order.png",
        selectedIconPath: "./images/order_act.png"
      },
      {
        text: '我的',
        name: 'user',
        pagePath: "/pages/user/index",
        iconPath: "./images/user.png",
        selectedIconPath: "./images/user_act.png"
      }
    ]
  },

  observers : {
    selectedIndex : function(i){

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    switchTab (e) {
      const index = e.currentTarget.dataset.index
      if (index == 2) {
        wx.scanCode({
          success: res => {
            let params = queryParams(res.result)
            let deviceNo = params.deviceNo
            let portNo = params.portNo || 0
            wx.navigateTo({ url: '/pages/charge/index?deviceNo=' + deviceNo + '&portNo=' + portNo})
          }
        })
      }else{
        let tab = this.data.tabList[index]
        wx.switchTab({
          url: tab.pagePath
        })
        this.setData({
          selectedIndex: index
        })
      }
      
    },

    publish () {
      wx.navigateTo({
        url: '/pages/publish/type/index'
      })
    }
  }
})
